package com.bwt.top.bwt_ad_flutter.ad;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bwt.top.UnifiedADContainer;
import com.bwt.top.ad.bean.UnifiedAdInfo;
import com.bwt.top.ad.impl.UnifiedAD;
import com.bwt.top.ad.listener.UnifiedADListener;
import com.bwt.top.bwt_ad_flutter.AdErrorUtil;
import com.bwt.top.bwt_ad_flutter.R;
import com.bwt.top.bwt_ad_flutter.config.BwtAdEventConfig;
import com.bwt.top.bwt_ad_flutter.manager.BwtAdManager;
import com.bwt.top.bwt_ad_flutter.model.AdParams;
import com.bwt.top.bwt_ad_flutter.util.LogUtil;
import com.bwt.top.exception.AdError;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


public class BwtUnifiedAd extends UnifiedADListener {

    private final AdParams adParams;
    private UnifiedAD unifiedAd;

    public UnifiedADContainer adContainer;
    private UnifiedAdInfo unifiedAdInfo;
    private Activity ac;

    public BwtUnifiedAd(AdParams adParams, Activity activity){
        this.adParams = adParams;
        this.ac = activity;
        this.unifiedAd = new UnifiedAD(activity);
    }

    public void loadAd(){
        this.unifiedAd.setAdId(adParams.getPosId());
        this.unifiedAd.setAdListener(this);
        this.unifiedAd.loadAd();
    }
    @Override
    public void onAdReceive(UnifiedAdInfo unifiedAdInfo) {
        this.unifiedAdInfo = unifiedAdInfo;
        LayoutInflater inflater = LayoutInflater.from(this.ac);
        this.adContainer = (UnifiedADContainer) inflater.inflate(R.layout.unifiedlayout, null, false);
        float density = this.ac.getResources().getDisplayMetrics().density;

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                (int) (adParams.getAdWidth()*density),
                (int)(adParams.getAdHeight()*density)
        );
        this.adContainer.setLayoutParams(params);
        unifiedAdInfo.bindAdToView(null,this.adContainer,Arrays.asList(this.adContainer));

//        TextView titleView = this.adContainer.findViewById(R.id.unified_container_title_tv);
//
//        titleView.setText(unifiedAdInfo.getTitle());

        ImageView imageView = this.adContainer .findViewById(R.id.unified_container_img);
        imageView.setMinimumWidth((int) (adParams.getAdWidth()*density));
        imageView.setMinimumHeight((int)(adParams.getAdHeight()*density));
        String imageUrl = unifiedAdInfo.getImgUrl();

        Glide.with(this.ac)
                .load(imageUrl)// 加载失败时显示的图片
                .diskCacheStrategy(DiskCacheStrategy.ALL) // 缓存策略
                .into(imageView);
        Log.d("自渲染Ad","开始加载广告--"+unifiedAdInfo);
        Map<String, Object> loadNativeAd = Map.of(
                "title",unifiedAdInfo.getTitle(),
                "desc",unifiedAdInfo.getDesc(),
                "imageUrl",unifiedAdInfo.getImgUrl(),
                "imageUrlAry",unifiedAdInfo.getImgList() == null ? List.of() : unifiedAdInfo.getImgList(),
                "iconImageUrl",unifiedAdInfo.getIconUrl()
        );
        BwtAdManager.getInstance().postNativeAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_LOAD, adParams,loadNativeAd);
    }
    //关闭广告
    public  void closeAd(){
        this.unifiedAdInfo.release();
        LogUtil.log("广告被关闭，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_CLOSE, adParams);
        release();
    }

    @Override
    public void onADLoaded(UnifiedAdInfo unifiedAdInfo) {

    }

    @Override
    public void onAdClick(UnifiedAdInfo unifiedAdInfo, String s) {
        LogUtil.log("广告被点击，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_CLICK, adParams);
    }

    @Override
    public void onAdExpose(UnifiedAdInfo unifiedAdInfo) {
        LogUtil.log("广告被展示，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_EXPOSE, adParams);
    }

    @Override
    public void onAdClose(UnifiedAdInfo unifiedAdInfo) {
        LogUtil.log("广告被关闭，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_CLOSE, adParams);
        release();
    }
    @Override
    public void onAdFailed(AdError adError) {
        LogUtil.log("广告获取失败，adId: " + adParams.getAdId() + ", error: " + AdErrorUtil.getError(adError));
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_FAILED, adParams, AdErrorUtil.getError(adError));
    }
    public AdParams getAdParams() {
        return adParams;
    }

    public UnifiedAD getUnifiedAd() {
        return unifiedAd;
    }

    public void setUnifiedAd(UnifiedAD unifiedAd) {
        this.unifiedAd = unifiedAd;
    }

    public UnifiedADContainer getAdContainer() {
        return adContainer;
    }

    public void setAdContainer(UnifiedADContainer adContainer) {
        this.adContainer = adContainer;
    }


    public void release() {
        if (unifiedAd != null) {
            this.adContainer.removeAllViews();
            this.adContainer = null;
            unifiedAd.release();
            unifiedAd = null;
        }
    }
}
